using System;
using System.Collections.Generic;
using System.Threading;
using System.Windows.Forms;
using Preparation;
using Tester.Forms;

namespace Tester.GuiActions.Actions.Impl
{
    public class TranslateAction :BaseAction
    {
        public override bool Visible
        {
            get
            {
                return
                    ( TesterMultyDocument.Instance != null && 
                      TesterMultyDocument.Instance.Suspended == TesterMultyDocument.SuspendStates.Stopped&&
                      TesterList.Instance != null &&  
                      TesterList.Instance.SelectedItems.Count > 0 &&
                      TesterMainForm.Instance.TopControl == EmbededControlTags.TesterList) ||
                    ( TesterMainForm.Instance.TopControl == EmbededControlTags.Properties &&
                      AlternativeView.Instance != null &&
                      AlternativeView.Instance.TestList.SelectedItem != null &&
                      TesterMultyDocument.Instance.Suspended == TesterMultyDocument.SuspendStates.Stopped);
            }
        }

        public override void MainAction(object sender, EventArgs e)
        {
            if (Visible)
            {
                if (TesterMainForm.Instance.TopControl == EmbededControlTags.TesterList)
                {
                    TesterMultyDocument.Instance.Suspended = TesterMultyDocument.SuspendStates.Translation;
                    List<Test> testList = new List<Test>();
                    foreach (ListViewItem it in TesterList.Instance.SelectedItems)
                    {
                        if (it.Tag != null)
                        {
                            testList.Add((Test) it.Tag);
                        }
                    }
                    Thread th = new Thread(new ParameterizedThreadStart(TesterList.Instance.translate));
                    th.Start(testList);
                } else
                {
                    if (AlternativeView.Instance != null &&
                        AlternativeView.Instance.TestList.SelectedItem!= null)
                    {
                        TesterMultyDocument.Instance.Suspended = TesterMultyDocument.SuspendStates.Translation;
                        List<Test> testList = new List<Test>();
                        testList.Add((Test) AlternativeView.Instance.TestList.SelectedItem);
                        Thread th = new Thread(new ParameterizedThreadStart(TesterList.Instance.translate));
                        th.Start(testList);
                    }
                }
            }
        }
    }
}
